package org.springleaf.core.mvc.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springleaf.core.exception.CrudException;
import org.springleaf.core.exception.CrudType;
import org.springleaf.core.query.QueryObject;

/**
 * 渲染Page的Controller
 * 
 * @author leaf
 * @param <T>
 */
public abstract class PageApiController<T> extends ApiController<T> {

	private String basePath;// 模型基本路径
	private String indexView;// 模型首页页面地址
	private String modelView;// 模型查看编辑页地址


	/**
	 * 得到Controller上注解着的路径(去掉了*),如果没有,则返回 /modelName
	 * 
	 * @return
	 */
	public String getControllPath() {
		RequestMapping mapping = this.getClass().getAnnotation(
				RequestMapping.class);
		String[] values = mapping.value();
		if (values != null && values.length > 0) {
			if (values[0].contains("*")) {
				values[0] = values[0].replaceAll("\\*", "");
			}
			return values[0];
		}
		return "/" + getBeanName();
	}

	/**
	 * 首页
	 */
	@RequestMapping
	public ModelAndView index(QueryObject queryObj) throws Exception {
		setAttr(getBeanName() + "List", queryModel(queryObj).getResult());
		return render(getIndexView());
	}

	/**
	 * 模型查看编辑页面
	 * 
	 * @param id
	 */
	@RequestMapping("{id}")
	public ModelAndView get(@PathVariable("id") Long id) throws Exception {
		T obj = getModel(id);
		if (obj != null) {
			setAttr(getBeanName(), obj);
			return render(getModelView());
		}
		throw new CrudException(CrudType.QUERY, "没有对应的数据");
	}

	/**
	 * 保存
	 * 
	 * @param obj
	 */
	@RequestMapping("save")
	public ModelAndView save(T obj) throws Exception {
		Object id = saveModel(obj);
		if (id != null) {
			return redirect(getBasePath());// 回到模型首页
		}
		throw new CrudException(CrudType.SAVE, "添加失败");
	}

	/**
	 * 更改
	 * 
	 * @param id
	 * @param obj
	 */
	@RequestMapping("update")
	public ModelAndView update(Long id, T obj) throws Exception {
		T model = updateModel(id, obj);
		if (model != null) {
			return redirect(getBasePath());
		}
		throw new CrudException(CrudType.UPDATE, "修改失败,不存在的数据");
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("delete/{id}")
	public ModelAndView delete(@PathVariable("id") Long id) throws Exception {
		try {
			deleteModel(id);
			return redirect(getBasePath());
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new CrudException(CrudType.DELETE, "删除失败");
		}
	}

	public String getBasePath() {
		if(this.basePath == null){
			basePath = getControllPath();
		}
		return basePath;
	}

	public void setBasePath(String basePath) {
		this.basePath = basePath;
	}

	public String getIndexView() {
		if(indexView == null)
			indexView = getBasePath() + "/" + getBeanName();
		return indexView;
	}

	public void setIndexView(String indexView) {
		this.indexView = indexView;
	}

	public String getModelView() {
		if(modelView == null)
			modelView = getBasePath() + "/view";
		return modelView;
	}

	public void setModelView(String modelView) {
		this.modelView = modelView;
	}

}
